import { defineStore } from 'pinia'
import { 
  getUserStats, 
  getBusinessStats, 
  getDivinationStats,
  getUserGrowthTrend,
  getUserRegionDistribution,
  getRevenueTrend,
  getProductSalesRanking,
  getDivinationTypeDistribution,
  getDivinationTimeDistribution
} from '@/api/stats'

export const useStatsStore = defineStore('stats', {
  state: () => ({
    userStats: {},
    businessStats: {},
    divinationStats: {},
    userGrowthTrend: [],
    userRegionDistribution: [],
    revenueTrend: [],
    productSalesRanking: [],
    divinationTypeDistribution: [],
    divinationTimeDistribution: [],
    loading: false
  }),

  actions: {
    // 获取用户统计数据
    async fetchUserStats(params) {
      this.loading = true
      try {
        const response = await getUserStats(params)
        this.userStats = response.data
        return response
      } catch (error) {
        console.error('获取用户统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取业务统计数据
    async fetchBusinessStats(params) {
      this.loading = true
      try {
        const response = await getBusinessStats(params)
        this.businessStats = response.data
        return response
      } catch (error) {
        console.error('获取业务统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取占卜统计数据
    async fetchDivinationStats(params) {
      this.loading = true
      try {
        const response = await getDivinationStats(params)
        this.divinationStats = response.data
        return response
      } catch (error) {
        console.error('获取占卜统计数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取用户增长趋势数据
    async fetchUserGrowthTrend(params) {
      this.loading = true
      try {
        const response = await getUserGrowthTrend(params)
        this.userGrowthTrend = response.data
        return response
      } catch (error) {
        console.error('获取用户增长趋势数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取用户地域分布数据
    async fetchUserRegionDistribution(params) {
      this.loading = true
      try {
        const response = await getUserRegionDistribution(params)
        this.userRegionDistribution = response.data
        return response
      } catch (error) {
        console.error('获取用户地域分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取收入趋势数据
    async fetchRevenueTrend(params) {
      this.loading = true
      try {
        const response = await getRevenueTrend(params)
        this.revenueTrend = response.data
        return response
      } catch (error) {
        console.error('获取收入趋势数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取产品销售排行数据
    async fetchProductSalesRanking(params) {
      this.loading = true
      try {
        const response = await getProductSalesRanking(params)
        this.productSalesRanking = response.data
        return response
      } catch (error) {
        console.error('获取产品销售排行数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取占卜类型分布数据
    async fetchDivinationTypeDistribution(params) {
      this.loading = true
      try {
        const response = await getDivinationTypeDistribution(params)
        this.divinationTypeDistribution = response.data
        return response
      } catch (error) {
        console.error('获取占卜类型分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    // 获取占卜时间分布数据
    async fetchDivinationTimeDistribution(params) {
      this.loading = true
      try {
        const response = await getDivinationTimeDistribution(params)
        this.divinationTimeDistribution = response.data
        return response
      } catch (error) {
        console.error('获取占卜时间分布数据失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    }
  }
})